home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Sample Code / Snippets / Toolbox / ListInDialog / ListInDialog.c next >
Encoding:
C/C++ Source or Header  |  1994-06-15  |  6.5 KB  |  240 lines  |  [TEXT/MPS ]

  1. /*
  2. ListInDialog
  3.  
  4. A snippet that shows how to (uuuhhhhh) put a list in a dialog.
  5.  
  6. I thought we had this one, but I guess not. It's easy, just create the list
  7. right after you create the dialog, then call LUpdate and LClick in a 
  8. dialog filter to respond to user events.
  9.  
  10. Please see the snippet DialogBits for a more comprehensive treatment of
  11. everything you can do in a dialog (well, almost).
  12.  
  13. This is a 680x0 version, built with the Universal Headers.
  14.  
  15. C.K. Haun
  16. April '94
  17. Are you sure we don't already have a snippet like this????
  18. Oh, I rememeber, that was on the //gs
  19.  
  20. */
  21.  
  22. #include <Dialogs.h>
  23. #include <Controls.h>
  24. #include <QuickDraw.h>
  25. #include <Windows.h>
  26. #include <ToolUtils.h>
  27. #include <OSUtils.h>
  28. #include <Menus.h>
  29. #include <Fonts.h>
  30. #include <resources.h>
  31. #include <Sound.h>
  32. #include <Traps.h>
  33. #include <Gestaltequ.h>
  34. #include <Memory.h>
  35. #include <Scrap.h>
  36. #include <TextEdit.h> 
  37. #include <Lists.h>
  38.  
  39. enum  {
  40.     kSampleDialog = 128
  41. };
  42. enum  {
  43.     kListItem = 3
  44. };
  45.  
  46. pascal Boolean theListFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
  47.  
  48. void SetUpList(ListHandle theList);
  49.  
  50. /* making the list handle a global */
  51. ListHandle gAList;
  52.  
  53. main()
  54. {
  55.     
  56.     // this "rect" defines the data bounds of the list. In this case, a
  57.     // one column list
  58.     Rect listRect2 =  {
  59.         0, 0, 0, 1
  60.     };
  61.     // cell to initialize with
  62.     Cell cp =  {
  63.         0, 0
  64.     };
  65.     
  66.     // the dialog we're using
  67.     DialogPtr myDialog = nil;
  68.     
  69.     // hitItem for ModalDialog call
  70.     short hitItem = 0;
  71.     
  72.     // these three are here for all the GetDItem/SetDItem calls
  73.     Rect tempRect;
  74.     short tempItem;
  75.     Handle tempHandle;
  76.     
  77.     // a temporary boolean (now THAT'S a helpful comment!)
  78.     Boolean tempBool;
  79.     
  80.     /* start up managers */
  81.     InitGraf((Ptr)&qd.thePort);
  82.     InitFonts();
  83.     InitWindows();
  84.     InitMenus();
  85.     TEInit();
  86.     InitDialogs(nil);
  87.     InitCursor();
  88.     
  89.     /* get our dialog. It is created HIDDEN, and shown after I set the */
  90.     /* user item that will hold the list */
  91.     
  92.     myDialog = GetNewDialog(kSampleDialog, nil, (WindowPtr)-1);
  93.     
  94.     // setting it up in the Dialog manager's records, 
  95.     GetDItem(myDialog, kListItem, &tempItem, &tempHandle, &tempRect);
  96.     
  97.     /* inset the rect by 16, which is the width of the scroll bar that will be attached */
  98.     /* to this list */
  99.     tempRect.right -= 16;
  100.     
  101.     /* set the current port to this dialog */
  102.     SetPort(myDialog);
  103.     
  104.     /* create the list */
  105.     
  106.     gAList = LNew(&tempRect,    // in the tempRect bounds
  107.         &listRect2,                // with a sinngle column
  108.         cp,                        // default cell size (a cell of 0,0 says that)
  109.         nil,                    // no special LDEF, use the standard one
  110.         myDialog,                // put it in this port
  111.         false,                    // do NOT draw initially
  112.         false,                    // does NOT have a grow box space
  113.         false,                    // does NOT scroll horizontally
  114.         true);                    // DOES have a verticle scroll bar
  115.     
  116.     // call another funtion to put in the strings
  117.     SetUpList(gAList);
  118.     
  119.     // turn drawing on after the list has been filled
  120.     LDoDraw(true, gAList);
  121.     
  122.     // show the dialog
  123.     ShowWindow((WindowPtr)myDialog);
  124.     
  125.     /* draw it once */
  126.     DrawDialog(myDialog);
  127.     
  128.     // loop until ModalDialog is done
  129.     do {
  130.         // we have to use a ModalDialog filter, since ModalDialog doesn't
  131.         // automatically handle lists
  132.         ModalDialog((ModalFilterProcPtr)theListFilter, &hitItem);
  133.         
  134.         // switch off what item was hit
  135.         switch (hitItem) {
  136.             /* I don't care about any of the hits here */
  137.             
  138.         }
  139.         // wait for an OK or Cancel
  140.     }
  141.             while (hitItem != ok && hitItem != cancel);
  142.     
  143.     // don't do anything in this sample
  144.     DisposeDialog(myDialog);
  145. }
  146.  
  147.  
  148. /* Here is the filter that handles any List activity */
  149. pascal Boolean theListFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  150. {
  151.     
  152.     Rect tempR;
  153.     Boolean returnValue = false;    // defaults to me not saying I handled anything
  154.     Boolean theBoolean;
  155.     WindowPtr oldP;
  156.  
  157.     // get the current port, set the port to this dialog
  158.     GetPort(&oldP);
  159.     SetPort(theDialog);
  160.     
  161.     // was this an update event for this dialog?
  162.     if (theEvent->what == updateEvt && theEvent->message == theDialog) {
  163.         
  164.         
  165.         // Update the list.
  166.         LUpdate(theDialog->visRgn, gAList);
  167.         
  168.         // get the list rectangle
  169.         tempR = (*gAList)->rView;
  170.         
  171.         // push it outwards one pixel and frame the list
  172.         InsetRect(&tempR, -1, -1);
  173.         FrameRect(&tempR);
  174.         
  175.         // NOTE: Do !NOT! return 'true' if you did SOME drawing in response to a dialog update
  176.         // event in your filter!
  177.         // ONLY if you did EVERYTHING should you return 'true', or else you can cause other updating
  178.         // not to occur, which would be Bad
  179.         
  180.     } else {
  181.         // see if this was a mouseDown event
  182.         if (theEvent->what == mouseDown) {
  183.             Point theP;
  184.  
  185.             // we set the port to the dialog on entry to the filter, so a GetMouse will work
  186.             GetMouse(&theP);
  187.             
  188.             // get the list rectangle
  189.             tempR = (*gAList)->rView;
  190.             
  191.             // add the scroll bar back in for hit testing (remember we took it out earlier)
  192.             tempR.right += 16;
  193.             
  194.             // See if they clicked in our list!
  195.             if (PtInRect(theP, &tempR)) {
  196.                 theBoolean = LClick(theP, nil, gAList);
  197.                 // if they double-clicked the list, return 1, as if the OK button had been pressed
  198.                 if (theBoolean)
  199.                     *itemHit = 1;
  200.                 else
  201.                     *itemHit = kListItem;
  202.                 
  203.                 // tell the Dialog Manager that we handled this click, it can stop searching for a click-owner
  204.                 returnValue = true;
  205.             }
  206.         }
  207.     }
  208.     
  209.     // reset the original port
  210.     
  211.     SetPort(oldP);
  212.     
  213.     return(returnValue);
  214. }
  215.  
  216.  
  217. /* simple function to put a list of strings into my list */
  218. void SetUpList(ListHandle theList)
  219. {
  220.     register qq;
  221.     Cell cp;
  222.     Str255 theString;
  223.     short listCount = 0;
  224.     Boolean Changed = false;
  225.     short theCount;
  226.     Handle tempHandle = GetResource('STR#', 128);
  227.     if(tempHandle){
  228.     theCount = *((short *)(*tempHandle));
  229.         if (theCount) {
  230.             for (qq = 0; qq < theCount; qq++) {
  231.                 GetIndString(theString, 128, qq + 1);
  232.                 listCount = LAddRow(1, listCount + 1, theList);
  233.                 cp.h = 0;
  234.                 cp.v = listCount;
  235.                 LAddToCell(&theString[1], theString[0], cp, theList);
  236.             }
  237.         }
  238.     }
  239. }
  240.